<template>
    <dl>
        <dt>
            <img src="./截图20250818092514.png" alt="" width="200px">
        </dt>
        <dd>
            <div class="box">
                <h2>欢迎注册</h2>
                <el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" :rules="rules" label-width="auto">
                    <el-form-item label="账号" prop="username">
                        <el-input v-model="ruleForm.username" />
                    </el-form-item>

                    <el-form-item label="密码" prop="password">
                        <el-input v-model="ruleForm.password" />
                    </el-form-item>

                    <el-form-item>
                        <el-button type="primary" @click="submitForm(ruleFormRef)">
                            注册
                        </el-button>

                    </el-form-item>
                </el-form>
            </div>
        </dd>
    </dl>


</template>

<script setup>
import { reactive, ref } from 'vue'
const ruleFormRef = ref()
const ruleForm = reactive({
    username: '',
    password: '',
})

const rules = reactive({
    username: [
        { required: true, message: '请输入账号', trigger: 'blur' },
        { min: 3, max: 8, message: ' 3 to 8', trigger: 'blur' },
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 3, max: 8, message: ' 3 to 8', trigger: 'blur' },
    ],
})

import http from '@/api/http'
import { ElMessage } from 'element-plus'
import router from '@/router'
const submitForm = async (formEl) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            http.post('/register', ruleForm).then(res => {
                console.log('注册', res);
                if (res.data.code == 10000) {
                    ElMessage({
                        message: '注册成功',
                        type: 'success',
                    })
                    router.push('/login')
                }
                else {
                    ElMessage.error('注册失败')

                }

            })
        } else {
            ElMessage.error('注册失败')

        }
    })
}
</script>

<style lang="scss" scoped>
.box {
    width: 300px;
    height: 300px;
    background-color: aqua;
    margin-top: 1px;
    margin-left: 1000px;

    .el-button {
        width: 100px;
        margin-left: 50px;
    }

    h2 {
        margin-left: 80px;
        color: red;
    }
}
.dl{
    display: flex;
}
</style>